package com.tim.app.ui.adapter.selceted;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.library.net.StringResponseCallback;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.google.gson.Gson;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.constant.AppKey;
import com.tim.app.server.api.API_User;
import com.tim.app.server.entry.Friend;
import com.tim.app.server.entry.User;
import com.tim.app.server.result.ActionResult;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.ui.activity.some_homepage.SomeoneHomepageActivity;
import com.tim.app.util.BitmapLoader;
import com.tim.app.util.ToastUtil;

import java.util.List;


/**
 * 邀请回答
 */
public class InviteToAnswerAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Friend> {

    static final String TAG = "InviteToAnswerAdapter";

    private Context mContext;
    private String problemId;
    private int type;

    public InviteToAnswerAdapter(Context mContext, List<Friend> mDataList, String problemId, int type) {
        super(mDataList);
        this.mContext = mContext;
        this.problemId = problemId;
        this.type = type;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_invite_to_answer, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewHolder mHolder, final int position, final Friend data) {
        if (data == null) {
            return;
        }
        final ViewHolder holder = (ViewHolder) mHolder;
        if (AppKey.INVITE_TYPE_FRIEND == type) {
            if (!TextUtils.isEmpty(data.getUserMsg().getImage())) {
                BitmapLoader.ins().loadImage(data.getUserMsg().getImage(),  holder.rivAvatar);
            }

            if (!TextUtils.isEmpty(data.getUserMsg().getName())) {
                holder.tvUserName.setText(data.getUserMsg().getName());
            } else {
                holder.tvUserName.setText("");
            }
            if (!TextUtils.isEmpty(data.getUserMsg().getIntroduce())) {
                holder.tvDesc.setText(data.getUserMsg().getIntroduce());
            } else {
                holder.tvDesc.setText("个人签名");
            }
            holder.tvLevel.setText(mContext.getString(R.string.userLevel, String.valueOf(data.getUserMsg().getLevelId())));
            if (data.getUserMsg().getSex() == 1) {
                Drawable manDrawable = mContext.getResources().getDrawable(R.drawable.ic_male);
                manDrawable.setBounds(0, 0, manDrawable.getMinimumWidth(), manDrawable.getMinimumHeight());
                holder.tvInfo.setCompoundDrawables(manDrawable, null, null, null);
                holder.tvInfo.setCompoundDrawablePadding((int) RT.getDensity() * 1);
                holder.tvInfo.setSelected(false);
            } else if (data.getUserMsg().getSex() == 2) {
                Drawable manDrawable = mContext.getResources().getDrawable(R.drawable.ic_female);
                manDrawable.setBounds(0, 0, manDrawable.getMinimumWidth(), manDrawable.getMinimumHeight());
                holder.tvInfo.setCompoundDrawables(manDrawable, null, null, null);
                holder.tvInfo.setCompoundDrawablePadding((int) RT.getDensity() * 1);
                holder.tvInfo.setSelected(true);
            }
            holder.tvInfo.setText(String.valueOf(data.getUserMsg().getAge()));
        } else if (AppKey.INVITE_TYPE_RECOMMAND == type) {
            if (!TextUtils.isEmpty(data.getImage())) {
                BitmapLoader.ins().loadImage(data.getImage(),  holder.rivAvatar);
            }
            if (!TextUtils.isEmpty(data.getName())) {
                holder.tvUserName.setText(data.getName());
            } else {
                holder.tvUserName.setText("");
            }
            if (!TextUtils.isEmpty(data.getIntroduce())) {
                holder.tvDesc.setText(data.getIntroduce());
            } else {
                holder.tvDesc.setText("个人签名");
            }
            holder.tvLevel.setText(mContext.getString(R.string.userLevel, String.valueOf(data.getLevelId())));
            if (data.getSex() == 1) {
                Drawable manDrawable = mContext.getResources().getDrawable(R.drawable.ic_male);
                manDrawable.setBounds(0, 0, manDrawable.getMinimumWidth(), manDrawable.getMinimumHeight());
                holder.tvInfo.setCompoundDrawables(manDrawable, null, null, null);
                holder.tvInfo.setCompoundDrawablePadding((int) RT.getDensity() * 1);
                holder.tvInfo.setSelected(false);
            } else if (data.getSex() == 2) {
                Drawable manDrawable = mContext.getResources().getDrawable(R.drawable.ic_female);
                manDrawable.setBounds(0, 0, manDrawable.getMinimumWidth(), manDrawable.getMinimumHeight());
                holder.tvInfo.setCompoundDrawables(manDrawable, null, null, null);
                holder.tvInfo.setCompoundDrawablePadding((int) RT.getDensity() * 1);
                holder.tvInfo.setSelected(true);
            }
            holder.tvInfo.setText(String.valueOf(data.getAge()));
        }
        holder.tvUserName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AppKey.INVITE_TYPE_FRIEND == type) {
                    SomeoneHomepageActivity.start(mContext, data.getUserMsg(), false);
                } else if (AppKey.INVITE_TYPE_RECOMMAND == type) {
                    User user = new User();
                    user.setId(data.getId());
                    user.setUserId(data.getId());
                    user.setImage(data.getImage());
                    user.setName(data.getName());
                    SomeoneHomepageActivity.start(mContext, user, false);
                }
            }
        });
        holder.rivAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AppKey.INVITE_TYPE_FRIEND == type) {
                    SomeoneHomepageActivity.start(mContext, data.getUserMsg(), false);
                } else if (AppKey.INVITE_TYPE_RECOMMAND == type) {
                    User user = new User();
                    user.setId(data.getId());
                    user.setUserId(data.getId());
                    user.setImage(data.getImage());
                    user.setName(data.getName());
                    SomeoneHomepageActivity.start(mContext, user, false);
                }
            }
        });
        if (null != data.getInvitationStart()) {
            holder.tvInviteToAnswer.setSelected(true);
            holder.tvInviteToAnswer.setText(mContext.getString(R.string.hasInvited));
        } else {
            holder.tvInviteToAnswer.setSelected(false);
            holder.tvInviteToAnswer.setText(mContext.getString(R.string.unInvited));
        }
        if (position == getDataList().size() - 1) {
            holder.vLine.setVisibility(View.GONE);
        } else {
            holder.vLine.setVisibility(View.VISIBLE);
        }
        holder.tvInviteToAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.tvInviteToAnswer.setEnabled(false);
                String userId = "";
                if (AppKey.INVITE_TYPE_RECOMMAND == type) {
                    userId = data.getId();
                } else if (AppKey.INVITE_TYPE_FRIEND == type) {
                    userId = data.getUserMsg().getUserId();
                }
                API_User.ins().inviteToAnswer(TAG, userId, problemId, new StringResponseCallback() {
                    @Override
                    public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                        holder.tvInviteToAnswer.setEnabled(true);
                        if (errCode == ResponseResult.ResultStatus.OK) {
                            ActionResult result = new Gson().fromJson(resultValue, ActionResult.class);
                            if (null != result && null != result.getRes()) {
                                getDataList().get(position).setInvitationStart(Integer.valueOf(result.getRes().getAction().getId()));
                                notifyItemChanged(position);
                            }
                        } else if (ResponseResult.ResultStatus.BE_ADD_BLACK == errCode) {
                            if(data.getUserMsg().getSex() == AppKey.SEX_MAN){
                                ToastUtil.showToast("由于对方的设置，你不能邀请他回答问题");
                            }else {
                                ToastUtil.showToast("由于对方的设置，你不能邀请她回答问题");
                            }
                        }
                        return false;
                    }
                });

            }
        });
    }

    public class ViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivAvatar;
        TextView tvUserName;
        ImageView ivVip;
        TextView tvInfo, tvLevel, tvDesc;
        TextView tvInviteToAnswer;
        View vLine;

        public ViewHolder(View itemView) {
            super(itemView);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
            tvLevel = (TextView) itemView.findViewById(R.id.tvLevel);
            ivVip = (ImageView) itemView.findViewById(R.id.ivVip);
            tvInfo = (TextView) itemView.findViewById(R.id.tvInfo);
            tvDesc = (TextView) itemView.findViewById(R.id.tvDesc);
            tvInviteToAnswer = (TextView) itemView.findViewById(R.id.tvInviteToAnswer);
            vLine = itemView.findViewById(R.id.vLine);
        }

    }

}
